﻿@COFE ?= {}

$ = jQuery
Link = COFE.Link

COFE.MIMETypes ?= {}
COFE.MIMETypes.EntryList = "application/cofews.list+xml"
COFE.MIMETypes.TypeInfo = "application/cofews.entrytype+xml"
COFE.MIMETypes.Metadata = "application/cofews.metadata+xml"
COFE.MIMETypes.Stream = "application/octet-stream"

COFE.signinUrl = "users/signIn"
COFE.signoutUrl = "users/signOut"
COFE.iconUrlAtb = "rel=icon"
COFE.defUrlAtb="rel=default"
COFE.jsonAtb = "json=true";


class utils    
  
  

  # Show message box
  showMessage : (content, caption) ->
    contentHtml = "<p style='padding:0px 5px'>" + content + "</p>"
    $('html').simpledialog2({ 
       mode: 'blank',
       headerText: caption,
       headerClose: true,
       blankContent : contentHtml    
    })        
     
  ###
  Path Related routines
  ###

  ###
  extractFilename : (path) ->
    idx = path.lastIndexOf('/')
    if idx is -1 
    then path 
    else path.substring(idx+1, path.length)
  ###  
 
  extractFilepath : (path) ->    
    idx = path?.lastIndexOf('/')
    if (idx isnt -1) then path[0..idx] else ""    

  extractFilename : (path) ->
    idx = path?.lastIndexOf('/')
    if (idx isnt -1) then path[idx+1..path.length] else path  

  extractHeaderFilename : (path) ->
    (if path is "" 
    then "Root" 
    else @extractFilename(path))

  sizeInK : (size) ->
    Math.ceil(size / 1024)

  extractFilepath : (path) ->
    if (path is "") then undefined
    else 
      (
        idx = path.lastIndexOf('/')
        if idx is -1 
        then "" 
        else path.substring(0, idx)
      )

  ##  Signin related.
  
  getCookie : (name) ->
    for cookie in document.cookie.split ';' when cookie and name is (cookie.split '=')[0]
      return decodeURIComponent cookie[(1 + name.length)...]
    null

  callUrl : (url, crediential, successFunc, errorFunc) ->    
    params = { 
      type : "POST" 
      url : url
      success : successFunc
      error : errorFunc
      }

    if (crediential isnt undefined)
      params.data = crediential    
    $.ajax(params)  

  # Sign in using the crediential in param string, e.g. crediential = $("#signin_form").serialize();
  # A cookie will then sent to the browser for other operations.
  # Return OK for success, other string for the error.
  signin : (crediential, successFunc, errorFunc) =>       
    @callUrl(COFE.signinUrl, crediential, 
    (data, textStatus, jqXHR) =>       
      successFunc()
    (data, textStatus, jqXHR) =>
      errorStr = (if (jqXHR is "Not Found")
      then ("Wrong login or password.")
      else ("Cannot connect to the server."))      
      errorFunc(errorStr)
    )    

  # Try to refresh using the signin cookie.
  # Return OK for success, otherwise required signin again.
  refresh : (successFunc, errorFunc) =>     
    @callUrl(COFE.signinUrl, "", 
    (data, textStatus, jqXHR) =>       
      successFunc()
    (data, textStatus, jqXHR) =>       
      errorFunc(jqXHR)
      )     

  # Try to signout using the singin cookie.
  # Return OK for success, otherwise signout failed.
  signout : (successFunc, errorFunc) =>     
    @callUrl(COFE.signoutUrl, "", 
    (data, textStatus, jqXHR) =>       
      successFunc()
    (data, textStatus, jqXHR) =>       
      errorFunc(jqXHR)
      ) 

COFE.utils = new utils()


  
